package com.jumi.microservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.mq.http.MQProducer;
import com.aliyun.mq.http.model.TopicMessage;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.exception.ONSClientException;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.config.RocketMqConfig;
import com.jumi.microservice.domain.AppMessageAll;
import com.jumi.microservice.domain.MessageBody;
import com.jumi.microservice.domain.SMSMessage;
import com.jumi.microservice.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @ClassName MessageTestController
 * @Author Abel Yang
 * @Date 2020/10/24/024 13:13
 **/
@Api(tags = "测试发消息")
@RestController
@RequestMapping("/message/send")
public class MessageTestController {

    private static final Logger log = LoggerFactory.getLogger(MessageTestController.class);

    @Resource
    private RocketMqConfig rocketMqConfig;

    @Resource
    private MQProducer producerHttp;

    @Resource
    private ProducerBean producerTcp;

    @Resource
    private IMessageService iMessageService;

    @ApiImplicitParams(value = {})
    @ApiOperation("发短信")
    @GetMapping("/sms")
    public ResponseResult sms(SMSMessage SMSMessage) {

        Map<String, String> params = new HashMap<>();
        params.put("code","12345");
        SMSMessage sms = new SMSMessage();
        sms.setReceiverMobilePhone("19806163162");
        sms.setSmtShortId("SMS_186945553");
        sms.setMessageParams(params);

        //生成消息
        TopicMessage pubMsg = new TopicMessage(JSONObject.toJSONString(sms).getBytes(), rocketMqConfig.getTag());
        // 同步发送消息，只要不抛异常就是成功
        TopicMessage pubResultMsg = producerHttp.publishMessage(pubMsg);

        // 同步发送消息，只要不抛异常就是成功
        System.out.println(new Date() + " Send mq message success. Topic is:" + rocketMqConfig.getTopic() + ", msgId is: " + pubResultMsg.getMessageId()
                + ", bodyMD5 is: " + pubResultMsg.getMessageBodyMD5());

        return ResponseResult.success();
    }

    @ApiImplicitParams(value = {})
    @ApiOperation("发短信tcp")
    @GetMapping("/smsTcp")
    public ResponseResult smsTcp(MessageBody query) {

//        Map<String, String> params = new HashMap<>();
//        params.put("code","12345");
//        MessageSMSDTO sms = new MessageSMSDTO();
//        sms.setReceiverMobilePhone("19806163162");
//        sms.setSmtShortId("SMS_186945553");
//        sms.setMessageParams(params);

        Message msg = new Message(
                rocketMqConfig.getTopic(),
                rocketMqConfig.getTag(),
                JSONObject.toJSONString(query).getBytes());

        msg.setKey("ORDERID_100");
        try {
            SendResult sendResult = producerTcp.send(msg);
            assert sendResult != null;
            System.out.println(sendResult);
        } catch (ONSClientException e) {
            System.out.println("发送失败");
            log.error(e.getMessage());
        }

        return ResponseResult.success();
    }


    @ApiImplicitParams(value = {@ApiImplicitParam(name = "MessageQuery", value = "推送参数", dataType = "com.jumi.microservice.domain.MessageQuery")})
    @ApiOperation("消息推送")
    @PostMapping("/smsCeshi")
    public ResponseResult smsCeshi(@RequestBody String jsnData, HttpServletRequest request) {
        MessageBody body = JSONObject.toJavaObject(JSONObject.parseObject(jsnData), MessageBody.class);
        iMessageService.messageSend(body);

//        AppMessageAll appMessageAll = new AppMessageAll();
//        appMessageAll.setSpuId(15);
//        appMessageAll.setTitle("标题测试");
//        appMessageAll.setMessageContent("内容测试xxxxxxxx");
//        iMessageService.messageAllSend(appMessageAll);
//        //生成消息
//        String s = JSONObject.toJSONString(query);
//        TopicMessage pubMsg = new TopicMessage(s.getBytes(), "php");
//        // 同步发送消息，只要不抛异常就是成功
//        TopicMessage pubResultMsg = producerHttp.publishMessage(pubMsg);
//
//        // 同步发送消息，只要不抛异常就是成功
//        System.out.println(new Date() + " Send mq message success. Topic is:" + rocketMqConfig.getTopic() + ", msgId is: " + pubResultMsg.getMessageId()
//                + ", bodyMD5 is: " + pubResultMsg.getMessageBodyMD5());

        return ResponseResult.success();
    }

    @ApiImplicitParams(value = {@ApiImplicitParam(name = "MessageQuery", value = "推送参数", dataType = "com.jumi.microservice.domain.MessageQuery")})
    @ApiOperation("所有设备消息推送")
    @PostMapping("/smsCeshiAll")
    public ResponseResult smsCeshiAll(@RequestBody String jsnData, HttpServletRequest request) {
        AppMessageAll body = JSONObject.toJavaObject(JSONObject.parseObject(jsnData), AppMessageAll.class);
        iMessageService.messageAllSend(body);
        return ResponseResult.success();
    }
}
